package com.neusoft.controller;



import java.util.List;

import com.neusoft.po.Cart;
import com.neusoft.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import com.neusoft.po.CommonResult;
import com.neusoft.po.Orders;
import com.neusoft.service.OrdersService;

//@CrossOrigin("*")
@RestController
@RequestMapping("/OrdersController")
@RefreshScope //开启动态刷新
public class OrdersController {

	@Autowired
	private OrdersService ordersService;

	@PostMapping("/createOrders/{tokenString}/{businessId}/{daId}/{orderTotal}/{coupon}/{points}")
	public CommonResult<Integer> createOrders(@PathVariable("tokenString") String tokenString,
											  @PathVariable("businessId") Integer businessId, @PathVariable("daId") Integer daId,
											  @PathVariable("orderTotal") Double orderTotal, @PathVariable("coupon") String coupon,
											  @PathVariable("points") Integer points) throws Exception {
		Orders orders = new Orders();
		orders.setBusinessId(businessId);
		orders.setDaId(daId);
		orders.setOrderTotal(orderTotal);
		orders.setCoupon(coupon);
		orders.setPoints(points);
		try {
			TokenUtil.verify(tokenString);
		}catch(Exception e){
			return new CommonResult(401, "Unauthorized", 0);
		}
		if(TokenUtil.verify(tokenString)) {
			String userId = TokenUtil.tokenUser(tokenString);
			orders.setUserId(userId);
			int orderId = ordersService.createOrders(orders);
			return new CommonResult(200, "success（10600）", orderId);
		}else {
			return new CommonResult(401, "Unauthorized", 0);
		}

	}

	@GetMapping("/getOrdersById/{orderId}")
	public CommonResult<Orders> getOrdersById(@PathVariable("orderId") Integer orderId) throws Exception {
		Orders orders = ordersService.getOrdersById(orderId);
		return new CommonResult(200, "success（10600）", orders);
	}

	@GetMapping("/listOrdersByUserId/{tokenString}")
	public CommonResult<List> listOrdersByUserId(@PathVariable("tokenString") String tokenString) throws Exception {
		try {
			TokenUtil.verify(tokenString);
		}catch(Exception e){
			return new CommonResult(401, "Unauthorized", 0);
		}
		if(TokenUtil.verify(tokenString)) {
			String userId = TokenUtil.tokenUser(tokenString);
			List<Orders> list = ordersService.listOrdersByUserId(userId);
			return new CommonResult(200, "success（10600）", list);
		}else {
			return new CommonResult(401, "Unauthorized", 0);
		}

	}

	// 更新订单完成信息
	@PutMapping("/finishOrders/{orderId}")
	public CommonResult finishOrders(@PathVariable("orderId") Integer orderId) throws Exception{
		Orders orders = new Orders();
		orders.setOrderId(orderId);
		int num = ordersService.finishOrders(orders);
		return new CommonResult(200, "success（10600）", num);
	}

	// 删除订单信息
	@DeleteMapping("/deleteOrders/{orderId}")
	public CommonResult deleteOrders(@PathVariable("orderId") Integer orderId) throws Exception{
		Orders orders = new Orders();
		orders.setOrderId(orderId);
		List<Orders>  list = ordersService.deleteOrders(orders);
		return new CommonResult(200, "success（10600）", list);
	}
}